package MyLinkedList;

import java.util.List;
import java.util.Stack;

public class MyLinkedList {
    static class ListNode {
        public int val;
        public ListNode prev;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode head;
    public ListNode last;

    //得到链表的长度
    public int size() {
        int len = 0;
        ListNode cur = head;
        while(cur != null) {
            cur = cur.next;
            len++;
        }
        return len;
    }

    //打印链表
    public void display() {
        ListNode cur = head;
        while(cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    //打印链表2 以下都是从后向前打印
    public void display1(ListNode head) {
        //ListNode cur = head;
        if(head != null) {
            display1(head.next);
            System.out.print(head.val + " ");
        }
        System.out.println();
    }

    //打印链表3
    public void display2(ListNode head) {
        //ListNode cur = head;
        if(head == null ) {
            return;
        }
        if(head.next == null) {
            System.out.println(head.val);
            return;
        }

        display2(head.next);
        System.out.println(head.val);
    }

    //打印链表4
    public void display3() {
        Stack<ListNode> stack = new Stack<>();
        ListNode cur = head;
        while(cur != null) {
            stack.push(cur);
            cur = cur.next;
        }

        while(!stack.empty()) {
            ListNode tmp = stack.pop();
            System.out.print(tmp.val + " ");
        }
        System.out.println();
    }

    //查找是否包含关键字key
    public boolean contains(int k) {
        ListNode cur = head;
        while(cur != null) {
           if(cur.val == k) {
               return true;
           }
            cur = cur.next;
        }
        return false;
    }

    //头插法
    public void addFist(int data) {
        ListNode newnode = new ListNode(data);
        if(head == null) {
            head = newnode;
            //因为这是头插，所以头插的第一个结点就是链表的尾巴结点
            last = newnode;
            return;
        }

        newnode.next = head;
        head.prev = newnode;
        head = newnode;

//        if(head == null) {
//            head = newnode;
//        }else {
//            newnode.next = head;
//            head.prev = newnode;
//            head = newnode;
//        }
    }

    //尾部插入
    public void addLast(int data) {
        ListNode newnode = new ListNode(data);
        if(head == null) {
            head = newnode;
            last = newnode;
            return;
        }

        newnode.prev = last;
        last.next = newnode;
        last = newnode;
    }

    //任意位置插入
    public void addInsert(int index,int data) {
        int size = size();
        if(index < 0 || index > size) {
            throw new IndexOutOfBounds("双向链表index不合法！");
        }
        if(index == 0) {
            addFist(data);
            return;
        }
        if(index == size) {
            addLast(data);
            return;
        }

        ListNode cur = head;
        while(index != 0) {
            cur = cur.next;
            index--;
        }

        ListNode newnode = new ListNode(data);
//        newnode.next = cur;
//        cur.prev.next = newnode;
//        newnode.prev = cur.prev;
//        cur.prev = newnode;
        newnode.next = cur;
        newnode.prev = cur.prev;
        cur.prev.next = newnode;
        cur.prev = newnode;
    }


    //删除第一次出现的关键字key
    public void remove(int key) {
        ListNode cur = head;
        while(cur != null) {
            if(cur.val == key) {
                //开始删除
                if(cur == head) {
                    //删除头节点
                    head = head.next;
                    //考虑只有一个结点的时候！！！！所以要判断是否为空，如果不判断，只有一个结点时会报错空指针异常
                    if(head != null) {
                        head.prev = null;
                    }else {
                        last = null;
                    }
                }else {
                    cur.prev.next = cur.next;
                    //不是尾巴结点的情况
                    if(cur.next != null) {
                        //cur.prev.next = cur.next;
                        cur.next.prev = cur.prev;
                    }else {
                        //尾巴结点的情况
                        //cur.prev.next = cur.next;
                        last = last.prev;
                    }
                }
                return;
            }else {
                cur = cur.next;
            }
        }
    }


    //删除所有结点
    public void removeAllKey(int key) {
        ListNode cur = head;
        while(cur != null) {
            if(cur.val == key) {
                //开始删除
                if(cur == head) {
                    //删除头节点
                    head = head.next;
                    //考虑只有一个结点的时候！！！！所以要判断是否为空，如果不判断，只有一个结点时会报错空指针异常
                    if(head != null) {
                        head.prev = null;
                    }else {
                        last = null;
                    }
                }else {
                    cur.prev.next = cur.next;
                    //不是尾巴结点的情况
                    if(cur.next != null) {
                        //cur.prev.next = cur.next;
                        cur.next.prev = cur.prev;
                    }else {
                        //尾巴结点的情况
                        //cur.prev.next = cur.next;
                        last = last.prev;
                    }
                }
                //cur = cur.next;
            }//else {
                //cur = cur.next;
            //}
            cur = cur.next;
        }
    }


    //情况所有结点
    public void clear() {
        ListNode cur = head;
        while(cur != null) {
            ListNode prevCur = cur.next;
            cur.prev = null;
            cur.next = prevCur;
        }
        head = null;
        last = null;
    }

}
